$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'modules/news/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 80, key: true},
            {label: '文章名称', name: 'title', index: 'title', width: 250},
            {label: '浏览数', name: 'countView', index: 'count_view', width: 110},
            {label: '评论数', name: 'countComment', index: 'count_comment', width: 110},
            {label: '类型', name: 'tagname', index: 'tagname', width: 80},
            { label: '状态', name: 'status', index: 'status', width: 90 , formatter: function(value, options, row){
                return value === "1" ?
                    '<span class="label label-success">发布</span>' :
                    '<span class="label label-danger">草稿</span>';
            }},
            { label: '评论', name: 'isComment', index: 'is_comment', width: 90 , formatter: function(value, options, row){
                return value === 1 ?
                    '<span class="label label-success">是</span>' :
                    '<span class="label label-danger">否</span>';
            }},
            { label: '推荐', name: 'isRecommend', index: 'is_recommend', width: 90 , formatter: function(value, options, row){
                return value === 1 ?
                    '<span class="label label-success">是</span>' :
                    '<span class="label label-danger">否</span>';
            }},
            {label: '排序', name: 'sort', index: 'sort', width: 80},
            {label: '审核状态', name: 'approveStatus', index: 'approve_status', width: 125, formatter: function(value, options, row){
                return value === 10 ?
                    '<span class="label label-success">是</span>' :
                    '<span class="label label-danger">否</span>';
            }},
            {label: '发布时间', name: 'publishTime', index: 'publish_time', width: 200},
            {label: '更新时间', name: 'updateTime', index: 'update_time', width: 200},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 200}
            ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        }
    });

});
var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            titles: null,
            status : null
        },
        showList: true,
        title: null,
        editorContent: '',
        news: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.news = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.news.id == null ? "modules/news/save" : "modules/news/update";
            var html = $('#summernote').summernote('code');
             vm.news.content=html;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.news),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                            editor.destroy();
                        });
                    } else {
                        alert(r.msg);
                        editor.destroy();
                    }
                }
            });

        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "modules/news/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "modules/news/info/" + id, function (r) {
                vm.news = r.news;
                $('#summernote').summernote('code', vm.news.content);
            });

        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:{'title': vm.q.titles,'status' : vm.q.status},
                page: page
            }).trigger("reloadGrid");
        }
    }
});